﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using exsportance.Silverlight.Common.Utils;
using System.Linq;
using System.Globalization;

namespace exsportance.Silverlight.Validate.Validators
{
    /// <summary>
    /// Validator that returns true if value exists in haystack
    /// </summary>
    public class InArrayValidator<T> : BaseValidator
    {
        #region members
        /// <summary>
        /// Validation failure message key for when the value not exists in haystack
        /// </summary>
        private const string NOT_IN_ARRAY = "notInArray";


        protected Dictionary<string, string> _messagesTemplates = new Dictionary<string, string>(){
        {InArrayValidator<T>.NOT_IN_ARRAY,"'%value%' was not found in the haystack"},
        };

        #endregion

        #region properties
        /// <summary>
        /// The value container
        /// </summary>
        public IEnumerable<T> Values { get; set; }
        #endregion

        #region constructor
        public InArrayValidator(IEnumerable<T> values)
            : base()
        {
            Values = values;
        }

        #endregion

        #region protected methods
        /// <summary>
        /// Returns true if if value exists in haystack
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        protected override bool CheckValid(object value)
        {
            rememberValue(value);

            T convertedValue = (T)Convert.ChangeType(value, typeof(T), CultureInfo.CurrentCulture.NumberFormat);

            if (!Values.Contains(convertedValue))
            {
                raiseMessage(InArrayValidator<T>.NOT_IN_ARRAY);
                return false;
            }

            return true;
        }

        protected override System.Collections.Generic.Dictionary<string, string> getMessageTemplates()
        {
            return _messagesTemplates;
        }
        #endregion
    }
}
